import Message.StuMessageDTO;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author liushan
 * Created by liushan on 2019-04-09.
 */
@WebFilter(filterName = "LoginFilter")
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {


        res.setContentType("application/json");
        res.setContentType("text/html;charset=UTF-8");
        System.out.println(this.getClass());

        HttpSession session = req.getSession(true);
        StuMessageDTO log = (StuMessageDTO)session.getAttribute("islogin");
        String sub = req.getParameter("submit");
        if(sub!=null&&sub.equals("退出")) {
            res.sendRedirect("https://cas.dgut.edu.cn/logout?callback=http://localhost:8080/DGUTLogin.jsp");
            session.invalidate();
        }
        else if(log!=null)
            res.sendRedirect("http://localhost:8080/DGUTLogin.jsp");

        else chain.doFilter(req,res);
    }
}